package team.seekers.hula.teamwork.todo.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import team.seekers.hula.common.annotation.ApiResponseBody;
import team.seekers.hula.common.entity.result.ApiExceptionEnum;
import team.seekers.hula.common.entity.result.ApiException;
import team.seekers.hula.auth.service.MindUtils;
import team.seekers.hula.teamwork.teamTodo.entity.vo.TeamTodoTeamListVO;
import team.seekers.hula.teamwork.todo.entity.dto.TodoMeAddPA;
import team.seekers.hula.teamwork.todo.entity.dto.TodoQuery;
import team.seekers.hula.teamwork.todo.entity.dto.TodoUpdatePA;
import team.seekers.hula.teamwork.todo.entity.vo.DayClassListVO;
import team.seekers.hula.teamwork.todo.entity.vo.TodoListVO;
import team.seekers.hula.teamwork.todo.service.TodoService;
import team.seekers.hula.user.user.entity.dao.UserDO;
import team.seekers.hula.user.user.service.UserService;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Map;

/**
 * 一个待办
 * @author hbc
 * @since 2023-01-05
 */

@Controller
@ApiResponseBody
@RequestMapping("/todo")
public class TodoController {

    @Resource
    private TodoService todoService;

    @Resource
    private UserService userService;


    @ApiOperation(value = "新建个人待办")
    @PostMapping("/add")
    public void add(@RequestBody TodoMeAddPA todoMeAddPA) {
        todoService.add(todoMeAddPA);
    }


    @ApiOperation(value = "逻辑删除个人待办")
    @GetMapping("/delete")
    public void delete(@RequestParam String id) {
        if (todoService.isTeamTodoId(id)){
            throw new ApiException(ApiExceptionEnum.USER_NO_PERMISSION, "不支持删除团队待办");
        }
        todoService.removeById(id);
    }

    @ApiOperation(value = "更新个人待办")
    @PostMapping("/update")
    public void update(@RequestBody TodoUpdatePA todoUpdatePA) {
        todoService.update(todoUpdatePA);
    }

    @ApiOperation(value = "完成待办")
    @GetMapping("/finish")
    public void finish(@RequestParam String id) {
        todoService.finish(id);
    }


    @ApiOperation(value = "恢复待办")
    @GetMapping("/recover")
    public void recover(@RequestParam String id) {
        todoService.recover(id);
    }


    @ApiOperation(value = "渲染个人待办")
    @PostMapping("/list-mine")
    public List<TodoListVO> todoList(@RequestBody TodoQuery todoQuery) {
        return todoService.todoList(todoQuery);
    }

    @ApiOperation(value = "渲染个人所有团队待办,分两层，上层是所有的团队，团队的VO中包含该团队所有未完成toto的list")
    @PostMapping("/list-team")
    public List<TeamTodoTeamListVO> todoTeamList(@RequestBody TodoQuery todoQuery) {
        return todoService.teamTodoList(todoQuery);
    }


    @ApiOperation(value = "渲染所有今日待办")
    @GetMapping("/list-today")
    public Map<String, Object> todayList() {
        String userId = MindUtils.getUserId();
        Map<String, Object> map = todoService.todayList(userId);
        UserDO user = userService.getById(userId);

        Calendar todayCalendar = Calendar.getInstance();
        int weekDay = todayCalendar.get(Calendar.DAY_OF_WEEK);
        if(StringUtils.isNotBlank(user.getClassTable())){
            JSONObject jsonObject = JSON.parseObject(user.getClassTable());
            List<List<List<Map<String,Object>>>> classTable = (List<List<List<Map<String,Object>>>>) jsonObject.get("classTable");
            List<List<Map<String,Object>>> dayClass = classTable.get(weekDay == 1 ? 6 : weekDay-2);


            ArrayList<DayClassListVO> dayClassListVOS = new ArrayList<>();
            for (List<Map<String, Object>> aClass : dayClass) {
                for (Map<String, Object> classMap : aClass) {
                    if (StringUtils.isNotBlank((String) classMap.get("teacher"))) {
                        DayClassListVO dayClassListVO = new DayClassListVO();
                        dayClassListVO.setClassName((String) classMap.get("name"));
                        dayClassListVO.setClassLocation((String) classMap.get("place"));

                        Integer start = (Integer) classMap.get("start");
                        Integer number = (Integer) classMap.get("number");

                        Integer[] startTimes = {8, 9, 10, 11, 14, 15, 16, 17, 19, 20};

                        dayClassListVO.setClassTimeIndex(start);
                        dayClassListVO.setClassTime(startTimes[start] + ":00 ~ " + (startTimes[start] + number) + ":00");
                        // 检测状态
                        Calendar calendar = Calendar.getInstance();
                        int hour = calendar.get(Calendar.HOUR_OF_DAY);
                        int minute = calendar.get(Calendar.MINUTE);
                        int now = hour * 60 + minute;

                        int startTime = startTimes[start] * 60;
                        int endTime = startTimes[start] * 60 + number * 60 - 45;

                        if (now < startTime) {
                            dayClassListVO.setClassStatus(0);
                        } else if (now < endTime) {
                            dayClassListVO.setClassStatus(1);
                        } else {
                            dayClassListVO.setClassStatus(2);
                        }

                        dayClassListVOS.add(dayClassListVO);
                    }
                }
            }
            map.put("dayClass",dayClassListVOS);
        }
        return map;
    }



    @ApiOperation(value = "分页查询所有已完成待办")
    @GetMapping("/page-finished-order-time")
    public List<TodoListVO> pageTodoFinished(@RequestParam Integer pageNow,
                                             @RequestParam Integer pageSize) {
        return todoService.pageTodoFinished(pageNow, pageSize);
    }


    @ApiOperation(value = "移入或移出今日待办")
    @GetMapping("/in-or-out-today")
    public void toToday(@RequestParam String id) {
        todoService.moveInOrOutToday(id);
    }
}

